package cn.me.alphamq.nameserver.config.cluster;

import cn.me.alphamq.common.util.PropertiesUtil;
import cn.me.alphamq.nameserver.enumeration.ReplicationRoleEnum;
import cn.me.alphamq.nameserver.enumeration.ReplicationTypeEnum;

import java.util.Properties;

/**
 * 主从复制的配置类
 *
 * @author f
 */
public class MasterSlaveReplicationProperties {

    /**
     * 主节点的 ip + 端口
     */
    private final String master;

    /**
     * 本节点在主从复制中扮演的角色
     *
     * @see ReplicationRoleEnum
     */
    private final ReplicationRoleEnum role;

    /**
     * 复制的类型
     *
     * @see ReplicationTypeEnum
     */
    private final ReplicationTypeEnum type;

    /**
     * 本节点向外开放的端口
     */
    private final Integer port;

    public MasterSlaveReplicationProperties(String master, String role, String type, Integer port) {
        this.master = master;
        this.role = ReplicationRoleEnum.of(role);
        this.type = ReplicationTypeEnum.of(type);
        this.port = port;
    }

    /**
     * 根据 properties 加载一个主从复制配置的对象
     *
     * @param properties 配置对象
     * @return 填充配置的对象
     */
    public static MasterSlaveReplicationProperties load(Properties properties) {
        return new MasterSlaveReplicationProperties(
                PropertiesUtil.getNotNullProperty(properties, "nameserver.replication.master"),
                PropertiesUtil.getNotNullProperty(properties, "nameserver.replication.master.slave.role"),
                PropertiesUtil.getNotNullProperty(properties, "nameserver.replication.master.slave.type"),
                PropertiesUtil.getIntProperty(properties, "nameserver.replication.master.slave.port")
        );
    }

    public String getMaster() {
        return master;
    }

    public ReplicationRoleEnum getRole() {
        return role;
    }

    public ReplicationTypeEnum getType() {
        return type;
    }

    public Integer getPort() {
        return port;
    }
}
